﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BKH.BLL;

namespace BKH.MyUserControl.UCHienThi
{
    public partial class UCHienThiDanhSachBN : UserControl
    {
        BenhNhanBUO bnbuo = new BenhNhanBUO();

        String ID_Co;
        public UCHienThiDanhSachBN()
        {
            InitializeComponent();
            ShowData();
            txtTimTheoTen.Enabled = true;
            txtTimTheoMaBN.Enabled = false;
            txtTimTheoCMND.Enabled = false;
            txtBHYT.Enabled = false;
        }

        private void groupBox1_Enter(object sender, EventArgs e)
        {

        }
        void ShowData()
        {
            dataGridView1.AutoGenerateColumns = false;
            dataGridView1.DataSource = bnbuo.GetAll();

        }
<<<<<<< .mine

        private void dataGridView1_MouseDown(object sender, MouseEventArgs e)
        {
            try
            {
                if (e.Button == System.Windows.Forms.MouseButtons.Right)
                {
                    DataGridView.HitTestInfo hit = dataGridView1.HitTest(e.X, e.Y);


                    // Only RowHeader and Cells (which make up a row) should fire a menu
                    if (hit.Type == DataGridViewHitTestType.RowHeader
                        || hit.Type == DataGridViewHitTestType.Cell
                        && hit.RowIndex >= 0)
                    {
                        // Notice how I assign the menu to the dataGridView below...

                        dataGridView1.ClearSelection();
                        dataGridView1.Rows[hit.RowIndex].Selected = true;
                        dataGridView1.ContextMenuStrip = contextMenuStrip1;
                        dataGridView1.ContextMenuStrip.Show(this.dataGridView1, new Point(e.X, e.Y));


                        ID_Co = dataGridView1.Rows[hit.RowIndex].Cells[0].Value.ToString();

                    }
                    // and then I remove the menu here...
                    // This is because after we assign it to the dataGridView
                    // it allows you to right-click anywhere on the control to
                    // get a menu.  Try removing this line and expirement.
                    dataGridView1.ContextMenuStrip = null;

                }
            }
            catch
            {
                contextMenuStrip1.Visible = false;
            }
        }

        private void đăngKýKhámToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ucDangKyKham k = new ucDangKyKham(ID_Co);
            ControlAcess.ShowConTrol(this.ParentForm, k, "gbHienThi");            
        }

        private void tbTimKiem_TextChanged(object sender, EventArgs e)
        {

        }

        private void txtTimKiem_TextChanged(object sender, EventArgs e)
        {
            

        }
        /// <summary>
        /// Tìm kiếm theo tên
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnTim_Click(object sender, EventArgs e)
        {
            //Tim kiem theo ten benh nhan

            String hoten = txtTimKiem.Text.Trim();
            String[] s = hoten.Split();
            String ten = "";
            for (int i = 1; i < s.Length; i++)
            {
                ten += " " + s[i];
            }
            dataGridView1.DataSource = bnbuo.Search(ten.Trim());
        }

        private void lịchSửKhámToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ucLichSuKham ls = new ucLichSuKham(int.Parse(ID_Co));
            ControlAcess.ShowConTrol(this.ParentForm, ls, "gbHienThi");
        }

        private void ngườiNhàBNToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ucThongTinNguoiNha info = new ucThongTinNguoiNha(int.Parse(ID_Co));
            ControlAcess.ShowConTrol(this.ParentForm, info, "gbHienThi");
        }
=======
<<<<<<< .mine

        private void dataGridView1_MouseDown(object sender, MouseEventArgs e)
        {
            try
            {
                if (e.Button == System.Windows.Forms.MouseButtons.Right)
                {
                    DataGridView.HitTestInfo hit = dataGridView1.HitTest(e.X, e.Y);


                    // Only RowHeader and Cells (which make up a row) should fire a menu
                    if (hit.Type == DataGridViewHitTestType.RowHeader
                        || hit.Type == DataGridViewHitTestType.Cell
                        && hit.RowIndex >= 0)
                    {
                        // Notice how I assign the menu to the dataGridView below...

                        dataGridView1.ClearSelection();
                        dataGridView1.Rows[hit.RowIndex].Selected = true;
                        dataGridView1.ContextMenuStrip = contextMenuStrip1;
                        dataGridView1.ContextMenuStrip.Show(this.dataGridView1, new Point(e.X, e.Y));


                        ID_Co = dataGridView1.Rows[hit.RowIndex].Cells[0].Value.ToString();

                    }
                    // and then I remove the menu here...
                    // This is because after we assign it to the dataGridView
                    // it allows you to right-click anywhere on the control to
                    // get a menu.  Try removing this line and expirement.
                    dataGridView1.ContextMenuStrip = null;

                }
            }
            catch
            {
                contextMenuStrip1.Visible = false;
            }
        }

        private void đăngKýKhámToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ucDangKyKham k = new ucDangKyKham(ID_Co);
            ControlAcess.ShowConTrol(this.ParentForm, k, "gbHienThi");            
        }

        private void tbTimKiem_TextChanged(object sender, EventArgs e)
        {

        }

        private void txtTimKiem_TextChanged(object sender, EventArgs e)
        {
            

        }
        /// <summary>
        /// Tìm kiếm theo tên
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnTim_Click(object sender, EventArgs e)
        {

            //Tim kiem theo ten benh nhanêm 
            if (radioButton1.Checked == true)
            {
                
                HospitalDBDataContext context=new HospitalDBDataContext();                
                dataGridView1.DataSource = context.tbl_BenhNhans.Select(n => new 
                { 
                    Ten = n.LastName + " " + n.FirstName,
                    BenhNhanID = n.BenhNhanID,
                    FirstName=n.FirstName,
                    LastName=n.LastName,
                    Address=n.Address,
                    Sex=n.Sex,
                    Tel=n.Tel,
                    Birthday=n.Birthday,
                    CMND=n.CMND,
                    BHYT=n.BHYT,
                    DateRegistered=n.DateRegistered
                }).Where(n => n.Ten.Contains(txtTimTheoTen.Text)).ToList();
            }
            //tim kiem theo ma benh nhan
            if (radioButton2.Checked == true)
            {

                dataGridView1.DataSource = bnbuo.Search(int.Parse(txtTimTheoMaBN.Text));
            }

            //Tim kiem theo CMND
            if (radioButton3.Checked == true)
            {

                dataGridView1.DataSource = bnbuo.SearchByCMND(txtTimTheoCMND.Text);
            }

            //Tìm theo BHYT
            if (radioButton4.Checked == true)
            {

                dataGridView1.DataSource = bnbuo.SearchByCMND(txtBHYT.Text);
            }

        }

        private void lịchSửKhámToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ucLichSuKham ls = new ucLichSuKham(int.Parse(ID_Co));
            ControlAcess.ShowConTrol(this.ParentForm, ls, "gbHienThi");
        }

        private void ngườiNhàBNToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                NguoiNhaBUO ngNha = new NguoiNhaBUO();
                tbl_ThanNhan obj = ngNha.GetSingle(int.Parse(ID_Co));
                if (obj.ThanNhanID!=null)
                {
                    ucThongTinNguoiNha info = new ucThongTinNguoiNha(int.Parse(ID_Co));
                    ControlAcess.ShowConTrol(this.ParentForm, info, "gbHienThi");
                }
                
            }
            catch
            {
                MessageBox.Show("Ko có thông tin người nhà của bệnh nhân này","Thông báo");
            }
            
        }

        private void radioButton1_CheckedChanged(object sender, EventArgs e)
        {
            txtTimTheoCMND.ReadOnly = true;
            txtTimTheoMaBN.ReadOnly = true;
            txtTimTheoTen.ReadOnly = false;
            txtBHYT.ReadOnly = true;
            txtTimTheoCMND.Text = "";
            txtTimTheoMaBN.Text = "";
            txtBHYT.Text = "";
        }

        private void radioButton2_CheckedChanged(object sender, EventArgs e)
        {
            txtTimTheoCMND.ReadOnly = true;
            txtBHYT.ReadOnly = true;
            txtTimTheoMaBN.ReadOnly = false;
            txtTimTheoMaBN.Enabled = true;
            txtTimTheoTen.ReadOnly = true;
            txtTimTheoCMND.Text = "";
            txtTimTheoTen.Text = "";
            txtBHYT.Text = "";
        }

        private void radioButton3_CheckedChanged(object sender, EventArgs e)
        {
            txtTimTheoCMND.ReadOnly = false;
            txtBHYT.ReadOnly = true;
            txtTimTheoCMND.Enabled = true;
            txtTimTheoMaBN.ReadOnly = true;
            txtTimTheoTen.ReadOnly = true;
            txtTimTheoMaBN.Text = "";
            txtTimTheoTen.Text = "";
            txtBHYT.Text = "";
        }

        private void radioButton4_CheckedChanged(object sender, EventArgs e)
        {
            txtTimTheoCMND.ReadOnly = true;
            
            txtTimTheoMaBN.ReadOnly = true;
            txtTimTheoTen.ReadOnly = true;
            txtBHYT.ReadOnly = false;
            txtBHYT.Enabled = true;
            txtTimTheoMaBN.Text = "";
            txtTimTheoTen.Text = "";
            txtBHYT.Text = "";
        }

        private void xóaToolStripMenuItem_Click(object sender, EventArgs e)
        {
            BenhNhanBUO bn = new BenhNhanBUO();           

            DialogResult ds = MessageBox.Show("Bạn có chắc chắn muốn xóa", "Xóa dữ liệu", MessageBoxButtons.YesNo);
            if (ds == DialogResult.Yes)
            {
                if (bn.Delete(int.Parse(ID_Co)))
                {
                    MessageBox.Show("Xóa thành công");
                }
                else
                {
                    MessageBox.Show("Xóa thất bại");
                }
                ShowData();
            }
            else
            {

            }
        }

        private void thêmBệnhNhânToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ucThemBenhNhan ucthem = new ucThemBenhNhan();
            ControlAcess.ShowConTrol(this.ParentForm, ucthem, "gbHienThi");
        }

        private void sửaThôngTinBNToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ucSuaThongTinBN sua = new ucSuaThongTinBN(int.Parse(ID_Co));
            ControlAcess.ShowConTrol(this.ParentForm,sua,"gbHienThi");
        }

        
=======

        private void dataGridView1_MouseDown(object sender, MouseEventArgs e)
        {
            try
            {
                if (e.Button == System.Windows.Forms.MouseButtons.Right)
                {
                    DataGridView.HitTestInfo hit = dataGridView1.HitTest(e.X, e.Y);


                    // Only RowHeader and Cells (which make up a row) should fire a menu
                    if (hit.Type == DataGridViewHitTestType.RowHeader
                        || hit.Type == DataGridViewHitTestType.Cell
                        && hit.RowIndex >= 0)
                    {
                        // Notice how I assign the menu to the dataGridView below...

                        dataGridView1.ClearSelection();
                        dataGridView1.Rows[hit.RowIndex].Selected = true;
                        dataGridView1.ContextMenuStrip = contextMenuStrip1;
                        dataGridView1.ContextMenuStrip.Show(this.dataGridView1, new Point(e.X, e.Y));


                        ID_Co = dataGridView1.Rows[hit.RowIndex].Cells[0].Value.ToString();

                    }
                    // and then I remove the menu here...
                    // This is because after we assign it to the dataGridView
                    // it allows you to right-click anywhere on the control to
                    // get a menu.  Try removing this line and expirement.
                    dataGridView1.ContextMenuStrip = null;

                }
            }
            catch
            {
                contextMenuStrip1.Visible = false;
            }
        }

        private void đăngKýKhámToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ucDangKyKham k = new ucDangKyKham(ID_Co);
            ControlAcess.ShowConTrol(this.ParentForm, k, "gbHienThi");            
        }

        private void tbTimKiem_TextChanged(object sender, EventArgs e)
        {

        }

        private void txtTimKiem_TextChanged(object sender, EventArgs e)
        {
            

        }
        /// <summary>
        /// Tìm kiếm theo tên
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnTim_Click(object sender, EventArgs e)
        {
            //Tim kiem theo ten benh nhan

            String hoten = txtTimKiem.Text.Trim();
            String[] s = hoten.Split();
            String ten = "";
            for (int i = 1; i < s.Length; i++)
            {
                ten += " " + s[i];
            }
            dataGridView1.DataSource = bnbuo.Search(ten.Trim());
        }

        private void lịchSửKhámToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ucLichSuKham ls = new ucLichSuKham(int.Parse(ID_Co));
            ControlAcess.ShowConTrol(this.ParentForm, ls, "gbHienThi");
        }

        private void ngườiNhàBNToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ucThongTinNguoiNha info = new ucThongTinNguoiNha(int.Parse(ID_Co));
            ControlAcess.ShowConTrol(this.ParentForm, info, "gbHienThi");
        }
>>>>>>> .r20
>>>>>>> .r21
    }
}
